{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0fbb3aec",
   "metadata": {
    "code_folding": []
   },
   "source": [
    "### 基本操作"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6920d4de",
   "metadata": {},
   "source": [
    "#### 创建数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "0f37ee8b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "arr = np.array([1,2,3]) \n",
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "392f75c0",
   "metadata": {},
   "outputs": [],
   "source": [
    "arr1 = np.ones(5)  #创建全为1的数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "547cd6fe",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1., 1., 1., 1., 1.])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "b653541c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 1., 1.],\n",
       "       [1., 1., 1.]])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr2 = np.ones(shape=(2,3)) \n",
    "arr2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "55d6eb7c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[0., 0., 0., 0.],\n",
       "        [0., 0., 0., 0.],\n",
       "        [0., 0., 0., 0.]],\n",
       "\n",
       "       [[0., 0., 0., 0.],\n",
       "        [0., 0., 0., 0.],\n",
       "        [0., 0., 0., 0.]]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr3 = np.zeros(shape=(2,3,4))  #创建全为0的数组\n",
    "arr3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "87c9092d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[5.8, 5.8, 5.8],\n",
       "       [5.8, 5.8, 5.8]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr4 = np.full(shape=(2,3), fill_value= 5.8) #指定填充的元素\n",
    "arr4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "b4fe932d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 4, 7])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr5 = np.arange(start=1, stop=10, step=3)  #等差数组\n",
    "arr5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "abc0a95e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1.        , 1.44444444, 1.88888889, 2.33333333, 2.77777778,\n",
       "       3.22222222, 3.66666667, 4.11111111, 4.55555556, 5.        ])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr6 = np.linspace(start=1, stop=5, num=10) #等差数组\n",
    "arr6"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "44cc6f42",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1.00000000e+002, 3.16227766e+026, 1.00000000e+051, 3.16227766e+075,\n",
       "       1.00000000e+100])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr7 = np.logspace(start=2, stop=100, num=5)\n",
    "arr7"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "c00fd772",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([60, 48, 49, 57, 73, 39,  7, 67, 95, 85])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr8 = np.random.randint(0,100,size=10)  #生成随机整数的数组\n",
    "arr8"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "8a504870",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.55635014, 0.55700384, 0.79680507, 0.06332675, 0.11508361])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr9 = np.random.random(5)  #生成随机数组\n",
    "arr9"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fb5c18de",
   "metadata": {},
   "source": [
    "#### 查看操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "2e4ec10e",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "377c4d94",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#查看维度\n",
    "arr1 = np.random.randint(0,30,size=(2,3,5))\n",
    "arr1.ndim"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "5b12d7a9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2, 3, 5)"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#数组形状和尺寸\n",
    "arr1.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "cd6ec30b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "30"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#元素总数\n",
    "arr1.size"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "a3833e0a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('int32')"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#数据类型\n",
    "arr1.dtype"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "bd911b6f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#数组中每个元素的大小，以字节为单位\n",
    "arr1.itemsize"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e91b38ca",
   "metadata": {},
   "source": [
    "#### 文件IO操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ee49bda8",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "ccbc9c6e",
   "metadata": {},
   "outputs": [],
   "source": [
    "#保存数组\n",
    "x = np.random.randn(5)\n",
    "y = np.arange(1,10, 10)\n",
    "#保存单个数组(保存到x_arr.npy文件)\n",
    "np.save(\"x_arr\",x)\n",
    "#保存多个数组\n",
    "np.savez(\"some_arry.npz\", xarr=x, yarr=y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "51a4d634",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-1.0350361 , -0.72291392, -1.59010509, -0.7259148 ,  0.19364055])"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#读取\n",
    "x1 = np.load(\"x_arr.npy\")\n",
    "x1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "2a25287b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1])"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y1 = np.load('some_arry.npz')['yarr']\n",
    "y1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "04895e31",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-1.0350361 , -0.72291392, -1.59010509, -0.7259148 ,  0.19364055])"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x2 = np.load('some_arry.npz')['xarr']\n",
    "x2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "347fd9f6",
   "metadata": {},
   "outputs": [],
   "source": [
    "#读写txt,csv文件\n",
    "arr1 = np.random.randint(0,10,size=(2,3))\n",
    "np.savetxt(\"arr1.txt\", arr1, delimiter=',')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "61e51094",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[7., 2., 4.],\n",
       "       [7., 7., 3.]])"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr2 = np.loadtxt('arr1.txt', delimiter=',')\n",
    "arr2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9885cb32",
   "metadata": {},
   "source": [
    "### 数据类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "160a4c48",
   "metadata": {},
   "outputs": [],
   "source": [
    "#int: int8、uint8、int16、int32、int64\n",
    "#float: float16、float32、float64\n",
    "#str"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "186ae62a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1., 2., 3.], dtype=float32)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "#创建array是指定类型\n",
    "arr1 = np.array([1,2,3], dtype='float32')\n",
    "arr1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "115c1bcc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1., 2., 3., 4., 5.], dtype=float32)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#asarray转换是指定类型\n",
    "arr2 = [1,2,3,4,5]\n",
    "arr3 = np.asarray(arr2, dtype='float32')\n",
    "arr3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "1ef4a61c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 4, 4, 6, 8], dtype=int16)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#astype数据类型转换\n",
    "arr4 = np.random.randint(0,10, size=5, dtype='int16')\n",
    "arr4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "cde9a764",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1., 4., 4., 6., 8.], dtype=float32)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr5 = arr4.astype('float32')\n",
    "arr5"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5983eb31",
   "metadata": {},
   "source": [
    "### 数组运算"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a98bb8ed",
   "metadata": {},
   "source": [
    "#### 加减乘除幂运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "b18da62c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[6, 6],\n",
       "       [6, 0],\n",
       "       [2, 4]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([[8, 8],\n",
       "       [8, 2],\n",
       "       [4, 6]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import numpy as  np\n",
    "#与数的运算\n",
    "arr1 = np.random.randint(0,10, size=(3,2))\n",
    "arr2 = arr1 + 2\n",
    "display(arr1, arr2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "d7fa34ff",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[36, 36],\n",
       "       [36,  0],\n",
       "       [ 4, 16]])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#幂运算\n",
    "arr3 = arr1 ** 2\n",
    "arr3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "b9409bd8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[12.        , 12.        ],\n",
       "       [12.        ,  0.        ],\n",
       "       [ 1.33333333,  5.33333333]])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#除法\n",
    "arr4 = arr3 / 3\n",
    "arr4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "ed0238c0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[6, 6],\n",
       "       [6, 0],\n",
       "       [2, 4]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([[8, 0],\n",
       "       [4, 4],\n",
       "       [1, 9]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([[14,  6],\n",
       "       [10,  4],\n",
       "       [ 3, 13]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "#与数组的运算, 其他运算类似\n",
    "arr5 = np.random.randint(0,10, size=(3,2))\n",
    "arr6 = arr1 + arr5\n",
    "display(arr1, arr5, arr6)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6930d3f8",
   "metadata": {},
   "source": [
    "#### 逻辑运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "f5c70570",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ True,  True],\n",
       "       [ True, False],\n",
       "       [False, False]])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# >,<.>=, <=, == \n",
    "arr7 = arr1 > 5\n",
    "arr7"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1b172d38",
   "metadata": {},
   "source": [
    "#### 赋值运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "9aa41854",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([3, 8, 6])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# +=, -=, *= ,不支持/= , 在原值的基础上做操作，会改变原来数组的值\n",
    "arr8 = np.array([2,7,5])\n",
    "arr8 += 1\n",
    "arr8"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b6977fc9",
   "metadata": {},
   "source": [
    "### 赋值和视图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "4bb7d2c0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([100,   4,   5,   1,   2])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([100,   4,   5,   1,   2])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import numpy as np\n",
    "#1.完成不复制\n",
    "a = np.random.randint(0,10, size=5)\n",
    "b = a #a和b指向的是同一块内存\n",
    "b[0] = 100\n",
    "display(a,b, a is b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "0773d06d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n",
      "True\n",
      "False\n",
      "True\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([200,   5,   8,   1,   6])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([200,   5,   8,   1,   6])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "#2,查看或者浅拷贝\n",
    "a = np.random.randint(0,10, size=5)\n",
    "b = a.view() # 使用a中的数据创建一个新数组对象\n",
    "print(a is b)  # 返回False a和b是两个不同名字对应同一个内存对象\n",
    "print(b.base is a)  # 返回True，b视图的根数据和a一样\n",
    "print(b.flags.owndata)  # 返回False b中的数据不是其自己的\n",
    "print(a.flags.owndata)  # 返回True a中的数据是其自己的\n",
    "b[0] = 200\n",
    "display(a,b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "6c8cc435",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n",
      "False\n",
      "True\n",
      "True\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([2, 3, 6, 2, 9])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([200,   3,   6,   2,   9])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "#3.深拷贝\n",
    "a = np.random.randint(0,10, size=5)\n",
    "b = a.copy()\n",
    "print(a is b) #False\n",
    "print(b.base is a)  # False\n",
    "print(b.flags.owndata)  # 返回True\n",
    "print(a.flags.owndata)  # 返回True\n",
    "b[0] = 200\n",
    "display(a,b)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cc3f87f4",
   "metadata": {},
   "source": [
    "### 索引、切片和迭代"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "2acf3fd5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n",
      "[5 6 7]\n",
      "[5 6 7 8 9]\n",
      "[0 3 6 9]\n",
      "[9 7 5 3 1]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([ 0,  1,  2,  3,  4,  5, 16,  7,  8,  9])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "#numpy中数组切片是原始数组的视图，这意味着数据不会被复制，视图上任何数据的修改都会反映到原数组上.\n",
    "#索引和切片\n",
    "#arr[start:stop:step]  默认start=0, stop=length, step=1\n",
    "arr = np.array([0,1,2,3,4,5,6,7,8,9])\n",
    "print(arr[5]) # 5\n",
    "print(arr[5:8]) # 5,6,7\n",
    "print(arr[5::]) #5,6,7,8,9\n",
    "print(arr[::3]) #0,3,6,9\n",
    "print(arr[::-2]) #倒序\n",
    "temp = arr[5:8]\n",
    "temp[1] = 16\n",
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "8a85ccb3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n",
      "[[3 5]\n",
      " [4 6]]\n",
      "[[ 3  5]\n",
      " [-7 -9]]\n"
     ]
    }
   ],
   "source": [
    "#二维数组的索引和切片\n",
    "arr2 = np.array([[1,3,5],[2,4,6],[-2,-7,-9],[6,6,6]])\n",
    "print(arr2[0,-1]) #5\n",
    "print(arr2[:2,-2:]) #[3,5],[4,6]\n",
    "print(arr2[::2,1::])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "a77b4287",
   "metadata": {},
   "outputs": [],
   "source": [
    "#花式索引和索引技巧"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "51db315e",
   "metadata": {},
   "source": [
    "### 形状操作"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cbea7177",
   "metadata": {},
   "source": [
    "#### 数组变形--reshape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "735864d6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[8 1 0 0 8]\n",
      "  [0 0 9 5 5]\n",
      "  [2 9 1 5 1]\n",
      "  [0 7 0 3 4]]\n",
      "\n",
      " [[5 9 1 2 4]\n",
      "  [1 2 5 6 1]\n",
      "  [4 7 8 1 9]\n",
      "  [1 0 1 3 3]]\n",
      "\n",
      " [[7 2 0 8 1]\n",
      "  [4 6 6 2 4]\n",
      "  [9 2 5 3 1]\n",
      "  [2 5 1 1 4]]]\n",
      "[[8 1 0 0 8 0 0 9 5 5 2 9]\n",
      " [1 5 1 0 7 0 3 4 5 9 1 2]\n",
      " [4 1 2 5 6 1 4 7 8 1 9 1]\n",
      " [0 1 3 3 7 2 0 8 1 4 6 6]\n",
      " [2 4 9 2 5 3 1 2 5 1 1 4]]\n",
      "[[8 1 0 0 8]\n",
      " [0 0 9 5 5]\n",
      " [2 9 1 5 1]\n",
      " [0 7 0 3 4]\n",
      " [5 9 1 2 4]\n",
      " [1 2 5 6 1]\n",
      " [4 7 8 1 9]\n",
      " [1 0 1 3 3]\n",
      " [7 2 0 8 1]\n",
      " [4 6 6 2 4]\n",
      " [9 2 5 3 1]\n",
      " [2 5 1 1 4]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "arr1 = np.random.randint(0,10,size = (3,4,5))\n",
    "print(arr1)\n",
    "arr2 = arr1.reshape(5,12) #返回新数组\n",
    "print(arr2) \n",
    "arr3 = arr1.reshape(-1,5)  # 自动“整形”，自动计算\n",
    "print(arr3)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d2a5f1c4",
   "metadata": {},
   "source": [
    "#### 数组转置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "57eea829",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[6, 0, 3],\n",
       "       [6, 1, 6]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([[6, 6],\n",
       "       [0, 1],\n",
       "       [3, 6]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "arr1 = np.random.randint(0,10, size=(2,3))\n",
    "display(arr1, arr1.T)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "9ca6c6e4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[7, 5, 8, 9],\n",
       "        [9, 1, 7, 8],\n",
       "        [5, 0, 3, 8],\n",
       "        [0, 7, 0, 3],\n",
       "        [0, 5, 2, 9],\n",
       "        [6, 2, 1, 4]],\n",
       "\n",
       "       [[9, 1, 5, 3],\n",
       "        [0, 1, 9, 3],\n",
       "        [0, 6, 5, 7],\n",
       "        [5, 2, 3, 5],\n",
       "        [5, 8, 1, 0],\n",
       "        [6, 0, 3, 7]],\n",
       "\n",
       "       [[1, 6, 7, 2],\n",
       "        [4, 5, 4, 0],\n",
       "        [4, 6, 8, 5],\n",
       "        [8, 3, 6, 3],\n",
       "        [0, 5, 5, 0],\n",
       "        [5, 1, 6, 2]]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([[[7, 9, 5, 0, 0, 6],\n",
       "        [9, 0, 0, 5, 5, 6],\n",
       "        [1, 4, 4, 8, 0, 5]],\n",
       "\n",
       "       [[5, 1, 0, 7, 5, 2],\n",
       "        [1, 1, 6, 2, 8, 0],\n",
       "        [6, 5, 6, 3, 5, 1]],\n",
       "\n",
       "       [[8, 7, 3, 0, 2, 1],\n",
       "        [5, 9, 5, 3, 1, 3],\n",
       "        [7, 4, 8, 6, 5, 6]],\n",
       "\n",
       "       [[9, 8, 8, 3, 9, 4],\n",
       "        [3, 3, 7, 5, 0, 7],\n",
       "        [2, 0, 5, 3, 0, 2]]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "#改变维度\n",
    "arr2 = np.random.randint(0,10,size = (3,6,4)) # shape(3,6,4)\n",
    "arr3 = np.transpose(arr2,axes=(2,0,1)) # transpose改变数组维度 shape(4,3,6)\n",
    "display(arr2, arr3)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6940ef9e",
   "metadata": {},
   "source": [
    "#### 数组堆叠"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "d1691fe1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3]\n",
      " [4 5 6]]\n",
      "[[1 2 3 4 5 6]]\n"
     ]
    }
   ],
   "source": [
    "#concatenate\n",
    "import numpy as np\n",
    "arr1 = np.array([[1,2,3]])\n",
    "arr2 = np.array([[4,5,6]])\n",
    "arr3 = np.concatenate([arr1, arr2], axis=0)\n",
    "arr4 = np.concatenate([arr1, arr2], axis=1)\n",
    "print(arr3)\n",
    "print(arr4)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "159c62f1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3 4 5 6]]\n",
      "[[1 2 3]\n",
      " [4 5 6]]\n"
     ]
    }
   ],
   "source": [
    "#hstack--水平方向重叠\n",
    "#vstack--竖直方向重叠\n",
    "arr5 = np.hstack([arr1, arr2])\n",
    "arr6 = np.vstack([arr1, arr2])\n",
    "print(arr5)\n",
    "print(arr6)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "45d2743b",
   "metadata": {},
   "source": [
    "#### 数组拆分split"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "abb667e2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[89, 35, 15, 63,  1],\n",
       "       [84, 26, 43, 92, 85],\n",
       "       [36, 11, 18, 40, 36],\n",
       "       [17, 14, 81, 43,  3],\n",
       "       [77, 59, 28, 29, 18],\n",
       "       [55, 78, 22, 81, 21]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "[array([[89, 35, 15, 63,  1],\n",
       "        [84, 26, 43, 92, 85],\n",
       "        [36, 11, 18, 40, 36]]),\n",
       " array([[17, 14, 81, 43,  3],\n",
       "        [77, 59, 28, 29, 18],\n",
       "        [55, 78, 22, 81, 21]])]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([[89, 35, 15, 63,  1],\n",
       "       [84, 26, 43, 92, 85],\n",
       "       [36, 11, 18, 40, 36],\n",
       "       [17, 14, 81, 43,  3],\n",
       "       [77, 59, 28, 29, 18],\n",
       "       [55, 78, 22, 81, 21]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "[array([[89, 35],\n",
       "        [84, 26],\n",
       "        [36, 11],\n",
       "        [17, 14],\n",
       "        [77, 59],\n",
       "        [55, 78]]),\n",
       " array([[15],\n",
       "        [43],\n",
       "        [18],\n",
       "        [81],\n",
       "        [28],\n",
       "        [22]]),\n",
       " array([[63,  1],\n",
       "        [92, 85],\n",
       "        [40, 36],\n",
       "        [43,  3],\n",
       "        [29, 18],\n",
       "        [81, 21]])]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "#split\n",
    "import numpy as np\n",
    "arr1 = np.random.randint(0,100, size=(6,5))\n",
    "arr2 = np.split(arr1, indices_or_sections=2, axis=0) # 在第一维（6）平均分成两份\n",
    "arr3 = np.split(arr1, indices_or_sections=[2,3], axis=1) # 在第二维（5）以索引2，3为断点分割成3份\n",
    "display(arr1, arr2)\n",
    "#print(arr3)\n",
    "display(arr1, arr3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "2134f70a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[array([[89],\n",
      "       [84],\n",
      "       [36],\n",
      "       [17],\n",
      "       [77],\n",
      "       [55]]), array([[35],\n",
      "       [26],\n",
      "       [11],\n",
      "       [14],\n",
      "       [59],\n",
      "       [78]]), array([[15, 63,  1],\n",
      "       [43, 92, 85],\n",
      "       [18, 40, 36],\n",
      "       [81, 43,  3],\n",
      "       [28, 29, 18],\n",
      "       [22, 81, 21]])]\n",
      "--------------------------\n",
      "[array([[89, 35, 15, 63,  1],\n",
      "       [84, 26, 43, 92, 85]]), array([[36, 11, 18, 40, 36],\n",
      "       [17, 14, 81, 43,  3]]), array([[77, 59, 28, 29, 18],\n",
      "       [55, 78, 22, 81, 21]])]\n"
     ]
    }
   ],
   "source": [
    "#水平分割 --hsplit\n",
    "#竖直分割 --vsplit\n",
    "arr4 =  np.hsplit(arr1, indices_or_sections=[1,2]) # 在水平方向，以索引1，2为断点分割成3份\n",
    "arr5 =  np.vsplit(arr1, indices_or_sections=3) #在竖直方向平均分割成3份\n",
    "print(arr4)\n",
    "print('--------------------------')\n",
    "print(arr5)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eabb5ccc",
   "metadata": {},
   "source": [
    "### 广播机制"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "aa9a2310",
   "metadata": {
    "code_folding": [
     0
    ]
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0 0 0]\n",
      " [1 1 1]\n",
      " [2 2 2]\n",
      " [3 3 3]]\n",
      "[[1 2 3]\n",
      " [2 3 4]\n",
      " [3 4 5]\n",
      " [4 5 6]]\n"
     ]
    }
   ],
   "source": [
    "#当两个数组的形状并不相同的时候，我们可以通过扩展数组的方法来实现相加、相减、相乘等操作，\n",
    "#这种机制叫做广播（broadcasting）\n",
    "import numpy as np\n",
    "\n",
    "#一维数组广播\n",
    "arr1 = np.sort(np.array([0,1,2,3]*3)).reshape(4,3) #shape(4,3)\n",
    "print(arr1)\n",
    "arr2 = np.array([1,2,3])\n",
    "print(arr1 + arr2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "aeb2e154",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 0, 0],\n",
       "       [1, 1, 1],\n",
       "       [2, 2, 2],\n",
       "       [3, 3, 3]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([[1],\n",
       "       [2],\n",
       "       [3],\n",
       "       [4]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([[1, 1, 1],\n",
       "       [3, 3, 3],\n",
       "       [5, 5, 5],\n",
       "       [7, 7, 7]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "#二维数组广播\n",
    "arr1 = np.sort(np.array([0,1,2,3]*3)).reshape(4,3) # shape(4,3)\n",
    "arr2 = np.array([[1],[2],[3],[4]]) # shape(4,1)\n",
    "arr3 = arr1 + arr2 # arr2 进行广播复制3份 shape(4,3)\n",
    "display(arr1, arr2, arr3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "c210bb55",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[0, 1],\n",
       "        [2, 3],\n",
       "        [4, 5],\n",
       "        [6, 7]],\n",
       "\n",
       "       [[0, 1],\n",
       "        [2, 3],\n",
       "        [4, 5],\n",
       "        [6, 7]],\n",
       "\n",
       "       [[0, 1],\n",
       "        [2, 3],\n",
       "        [4, 5],\n",
       "        [6, 7]]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([[0, 1],\n",
       "       [2, 3],\n",
       "       [4, 5],\n",
       "       [6, 7]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([[[ 0,  2],\n",
       "        [ 4,  6],\n",
       "        [ 8, 10],\n",
       "        [12, 14]],\n",
       "\n",
       "       [[ 0,  2],\n",
       "        [ 4,  6],\n",
       "        [ 8, 10],\n",
       "        [12, 14]],\n",
       "\n",
       "       [[ 0,  2],\n",
       "        [ 4,  6],\n",
       "        [ 8, 10],\n",
       "        [12, 14]]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "#三维数组广播\n",
    "arr1 = np.array([0,1,2,3,4,5,6,7]*3).reshape(3,4,2) #shape(3,4,2)\n",
    "arr2 = np.array([0,1,2,3,4,5,6,7]).reshape(4,2) #shape(4,2)\n",
    "arr3 = arr1 + arr2 # arr2数组在0维上复制3份 shape(3,4,2)\n",
    "display(arr1, arr2, arr3)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e2c1692b",
   "metadata": {},
   "source": [
    "### 通用函数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bb642d01",
   "metadata": {},
   "source": [
    "#### 元素级数字函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "77cdf25d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1.        , 2.        , 2.82842712, 3.        , 4.        ,\n",
       "       5.        ])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([  1,  16,  64,  81, 256, 625])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "#abs、sqrt、square、exp、log、sin、cos、tan，maxinmum、minimum、all、any、inner、\n",
    "#clip、round、trace、ceil、floor\n",
    "import numpy as np\n",
    "arr1 = np.array([1,4,8,9,16,25])\n",
    "arr2 = np.sqrt(arr1) #开方\n",
    "arr3 = np.square(arr1) #平方\n",
    "\n",
    "display(arr2, arr3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "e4ab0054",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 2,  4,  8,  9, 16, 16])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#clip\n",
    "arr4 = np.clip(arr1, 2, 16) #arr1中小于2的变为2，大于16的变为16\n",
    "arr4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "b55f6e5a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 5, 3, 9, 3, 9, 8])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#两数组中取大的值\n",
    "x = np.array([1,5,2,9,3,6,8])\n",
    "y = np.array([2,4,3,7,1,9,0])\n",
    "maxy = np.maximum(x,y)\n",
    "maxy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "e214762d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "39"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#inner向量内积\n",
    "arr1 = np.array([2,3,6])\n",
    "arr2 = np.array([3,1,5])\n",
    "arr3 = np.inner(arr1, arr2)\n",
    "arr3"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4f998d90",
   "metadata": {},
   "source": [
    "#### where函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "8c6cb231",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1,  3,  6,  7, 10])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#where 函数，三个参数，条件为真时选择值的数组，条件为假时选择值的数组\n",
    "#类似于三目运算\n",
    "arr1 = np.array([1,3,5,7,9])\n",
    "arr2 = np.array([2,4,6,8,10])\n",
    "arr3 = np.array([True, True, False, True, False])\n",
    "arr4 = np.where(arr3, arr1, arr2)\n",
    "arr4"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b6fc8bf2",
   "metadata": {},
   "source": [
    "#### 排序方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "37be7f79",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0,  1,  2,  3,  7,  9, 15])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([ 0,  1,  2,  3,  7,  9, 15])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([5, 6, 1, 0, 3, 4, 2], dtype=int64)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "#arr.sort()、np.sort()、arr.argsort()\n",
    "arr1 = np.array([3,2,15,7,9,0,1])\n",
    "arr2 = np.array([3,2,15,7,9,0,1])\n",
    "arr3 = np.array([3,2,15,7,9,0,1])\n",
    "arr1.sort() # 直接改变原数组\n",
    "arr4 = np.sort(arr2) #返回深拷贝排序结果\n",
    "arr5 = arr3.argsort() #返回从小到大排序索引\n",
    "\n",
    "display(arr1, arr4, arr5)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "002a57b5",
   "metadata": {},
   "source": [
    "#### 集合运算函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "ad245995",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([4, 6])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([2, 3, 4, 5, 6, 8])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([2, 8])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([3, 5])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "#交，并，差\n",
    "A = np.array([2,4,6,8])\n",
    "B = np.array([3,4,5,6])\n",
    "arr1 = np.intersect1d(A,B)\n",
    "arr2 = np.union1d(A,B)\n",
    "arr3 = np.setdiff1d(A,B)\n",
    "arr4 = np.setdiff1d(B,A)\n",
    "display(arr1, arr2, arr3, arr4)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c84c8867",
   "metadata": {},
   "source": [
    "#### 数学和统计函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "f17981e6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "88"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "16.8"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([[4],\n",
       "       [6]], dtype=int64)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([  1,   8,  10,  29,  52,  52, 140, 151, 157, 168])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([4.  , 3.5 , 5.  , 4.5 , 4.75])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([7.6, 3.8, 2.8, 3.2])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "#min、max、mean、median、sum、std、var、cumsum、cumprod、argmin、argmax、\n",
    "#argwhere、cov、corrcoef\n",
    "arr1 = np.array([1,7,2,19,23,0,88,11,6,11])\n",
    "a1 = arr1.min() #最小值\n",
    "a2 = arr1.max() #最大值\n",
    "a3 = arr1.mean() #平均值\n",
    "a4 = arr1.argmax() # 计算最大值的索引 返回 6\n",
    "a5 = np.argwhere(arr1 > 20) # 返回大于20的元素的索引\n",
    "a6 = np.cumsum(arr1) # 计算累加和\n",
    "arr2 = np.random.randint(0,10,size = (4,5))\n",
    "a7 = arr2.mean(axis = 0) # 计算列的平均值\n",
    "a8 = arr2.mean(axis = 1) # 计算行的平均值\n",
    "display(a1, a2, a3, a4, a5, a6, a7, a8)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a804c1c9",
   "metadata": {},
   "source": [
    "### 线性代数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "a97f305d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 25,  23],\n",
       "       [ -4, -11]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([[ 25,  23],\n",
       "       [ -4, -11]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from numpy import inv, \n",
    "#矩阵的乘积 -- dot或者@\n",
    "A = np.array([[4,2,3],\n",
    " [1,3,1]]) # shape(2,3)\n",
    "B = np.array([[2,7],\n",
    " [-5,-7],\n",
    " [9,3]]) # shape(3,2)\n",
    "display(np.dot(A,B))\n",
    "display(A @ B)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "58700253",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[-2.00000000e+00  5.00000000e-01  5.00000000e-01]\n",
      " [ 5.92118946e-16  2.00000000e+00 -1.00000000e+00]\n",
      " [ 1.00000000e+00 -1.50000000e+00  5.00000000e-01]]\n",
      "-2.0000000000000004\n"
     ]
    }
   ],
   "source": [
    "#逆矩阵 inv\n",
    "A = np.array([[1,2,3],\n",
    " [2,3,4],\n",
    " [4,5,8]]) # shape(3,3)\n",
    "print(np.linalg.inv(A)) #逆矩阵\n",
    "print(np.linalg.det(A)) #行列式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "85652999",
   "metadata": {},
   "outputs": [],
   "source": [
    "#特征值，特征向量，qr分解值，svd分解值"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.6"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "261px"
   },
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
